<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/1/12
 * Time: 8:58 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
jimport('joomla.html.pagination');

/**
 * Hosocanbos Model
 */
class VastModelHosocanbos extends JModelItem
{
    protected $hosocanbos;
    private $_total = null;
    private $_pagination = null;

    public function __construct()
    {
        parent::__construct();
        
        $config = JVastConfig::getConfig();
        
        $this->setState('limit', (isset($config['numOfItemsPerPage']) ? (int)$config['numOfItemsPerPage'] : JRequest::getVar('limit', 5, '', 'int')));
        $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));
        $session = JFactory::getSession();

        $hoten = JRequest::getString('hosocanboHoten', $session->get('hosocanbos.hoten'));
        $gioitinh = JRequest::getInt('hosocanboGioitinh', $session->get('hosocanbos.gioitinh'));
                
        $session->set('hosocanbos.hoten', $hoten);
        $session->set('hosocanbos.gioitinh', $gioitinh);
                
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
            
        $query->select('a.*');
        $query->from('#__hosocanbo AS a');

        if (!is_null($gioitinh) && $gioitinh != 2) {
            $query->where('a.gioitinh = ' . $gioitinh);
        }
            
        if (!is_null($hoten) && $hoten !== '') 
            $query->where('a.hoten like \'%' . $hoten . '%\'');
        elseif ($hoten === '')
            $query->where('a.hoten like \'%\'');
        
        $db->setQuery($query);
        
        $this->hosocanbos = $db->loadObjectList();
            
        $this->_total = count($this->hosocanbos);          
    }

    /**
     * Get the message
     * @return string The message to be displayed to the user
     */
    public function getHosocanbos()
    {
        $session = JFactory::getSession();
        $hoten = $session->get('hosocanbos.hoten');
        $gioitinh = $session->get('hosocanbos.gioitinh');

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $limit = $this->getState('limit');
        $limitstart = $this->getState('limitstart');

        $query->select('a.*');
        $query->from('#__hosocanbo AS a');

        if (!is_null($gioitinh) && $gioitinh != 2) {
            $query->where('a.gioitinh = ' . $gioitinh);
        }
            
        if (!is_null($hoten) && $hoten !== '') 
            $query->where('a.hoten like \'%' . $hoten . '%\'' . ' LIMIT ' . $limitstart . ',' . $limit);
        elseif ($hoten === '')
            $query->where('a.hoten like \'%\' LIMIT ' . $limitstart . ',' . $limit);
                    
        $db->setQuery($query);
        $this->hosocanbos = $db->loadObjectList();
                
        return $this->hosocanbos;
    }
    
    public function getPagination()
    {
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        return $this->_pagination;
    }

    public function getTotal()
    {
        return $this->_total;
    }
}